#include <bits/stdc++.h>

class Solution {
    stack<char> word;
    stack<int> num;
public:
    string decodeString(string s) {
        int len=s.length();
        string n1;
        string w1;
        int i=0,j=0,k=0;
        for(i=0;i<len;i++){
            if(s[i]>='0'&&s[i]<='9')
                n1+=s[i];
            else if(s[i]=='['){
                num.push(stoi(n1));
                n1="";
                word.push(s[i]);
            }
            else if(s[i]>='a'&&s[i]<='z'){
                word.push(s[i]);
            }
            else if(s[i]==']'){
                while(word.top()!='['){
                    w1+=word.top();
                    word.pop();
                }
                word.pop();
                for(j=0;j<num.top();j++){
                    for(k=w1.length()-1;k>=0;k--){
                        word.push(w1[k]);
                    }
                }
                num.pop();
                w1="";
            }
        }
        while(!word.empty()){
            w1+=word.top();
            word.pop();
        }
        reverse(w1.begin(),w1.end());
        return w1;
    }
};
